﻿using Client.ICommandNotify;
using Client.JsonDialog;
using Client.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.ViewModel
{
    public class BasicQuerySendToKdViewModel: NotifyBase
    {
        private readonly MessageDialog.Dialog dialog;
        private readonly SqlServer.DbContext dbContext;
        public BasicQuerySendToKdViewModel()
        {
            kDPM_Records = new List<KDPM_Record>();
            KDPM_Returns = new List<KDPM_Return>();
            KDPM_Logs = new List<KDPM_Log>();
            Kd_SendLogs = new List<Kd_SendLog>();
            QuerySendToKdCommand = new Command(QuerySendToKdCommandDoExecute, QuerySendToKdCommandCanExecute);
            ShowJsonCommand = new Command(ShowJsonCommandDoExecute, QuerySendToKdCommandCanExecute);
            dialog = HelpTools.IOC.Getobject<MessageDialog.Dialog>();
            dbContext = HelpTools.IOC.Getobject<SqlServer.DbContext>();
        }

       

        #region 方法
        private bool QuerySendToKdCommandCanExecute()
        {
            return string.IsNullOrWhiteSpace(Billid)==false;
        }

        private async void QuerySendToKdCommandDoExecute(object obj)
        {
            //Data Source=192.168.100.25;Initial Catalog=SRBG_Log;User ID=sa;Password=asdw@123
            dialog.OpenDialog=true;
         var data=  await Task.Run(() =>
            {
                dbContext.SqlConfigstr = "Data Source=192.168.100.25;Initial Catalog=SRBG_Basic;User ID=pcwp;Password=pcwpasdw@123;";
              var  kDPM_Records = dbContext.Where($"SELECT * FROM [dbo].[KDPM_Record] WHERE PMID='{Billid}'").Select().Select(o =>
                {
                    return new KDPM_Record
                    {
                        Data = o["Data"].ToString(),
                        LastSendTime = DateTime.Parse(o["LastSendTime"].ToString()),
                        ReplyStatus = Convert.ToInt32(o["ReplyStatus"]),
                        ReplyErrorMsg = o["ReplyErrorMsg"].ToString(),
                    };
                }).ToList();
              var  KDPM_Returns = dbContext.Where($"SELECT *FROM KDPM_Return WHERE pmid = '{Billid}' ").Select().Select(o =>
                {
                    return new KDPM_Return
                    {
                        type = o["type"]?.ToString(),
                        returnEmpName = o["returnEmpName"]?.ToString(),
                        returnInfo = o["returnInfo"]?.ToString(),
                        IsSuccess = Convert.ToInt16(o["IsSuccess"].ToString()),
                        ReplyTime = DateTime.Parse(o["ReplyTime"]?.ToString())
                    };
                }).ToList();
             var   KDPM_Logs = dbContext.Where($"SELECT * FROM KDPM_Log WHERE PMID = '{Billid}' ").Select().Select(o =>
                {
                    return new KDPM_Log
                    {
                        LogSource = o["LogSource"].ToString(),
                        Data = o["Data"].ToString(),
                        LogTime = DateTime.Parse(o["LogTime"].ToString())
                    };
                }).ToList();
             var   Kd_SendLogs = dbContext.Where($"SELECT * FROM Kd_SendLog WHERE BillId = '{Billid}' ").Select().Select(o =>
                {
                    return new Kd_SendLog
                    {
                        BillNo = o["BillNo"].ToString(),
                        State = Convert.ToInt32(o["State"].ToString()),
                        Desc = o["Desc"].ToString(),
                        CreateTime = DateTime.Parse(o["CreateTime"].ToString()),
                        LastSendTime = DateTime.Parse(o["LastSendTime"].ToString())

                    };
                }).ToList();
                return new
                {
                    kDPM_Records= kDPM_Records,
                    KDPM_Returns= KDPM_Returns,
                    KDPM_Logs= KDPM_Logs,
                    Kd_SendLogs= Kd_SendLogs
                };
            });
            kDPM_Records = data.kDPM_Records;
            KDPM_Returns = data.KDPM_Returns;
            KDPM_Logs = data.KDPM_Logs;
            Kd_SendLogs = data.Kd_SendLogs;
            dialog.OpenDialog = false;
        }

        private void ShowJsonCommandDoExecute(object obj)
        {
            var d=JsonConvert.SerializeObject(JsonConvert.DeserializeObject(obj as string), Formatting.Indented);
            new ShowJsonView(d).ShowDialog();
        }
        #endregion
        #region 命令
        public Command QuerySendToKdCommand { get; set; }
        public Command ShowJsonCommand { get; set; }

        #endregion

        #region 属性
        private List<KDPM_Record> kDPM_Records_;

        public List<KDPM_Record> kDPM_Records
        {
            get { return kDPM_Records_; }
            set { kDPM_Records_ = value;
                SetProperty(ref kDPM_Records_, kDPM_Records_);
            }
        }

        public List<KDPM_Return> KDPM_Returns_;
        public List<KDPM_Return> KDPM_Returns
        {
            get { return KDPM_Returns_; }
            set
            {
                KDPM_Returns_ = value;
                SetProperty(ref KDPM_Returns_, KDPM_Returns);
            }
        }
        public List<KDPM_Log> KDPM_Logs_;
        public List<KDPM_Log> KDPM_Logs
        {
            get { return KDPM_Logs_; }
            set
            {
                KDPM_Logs_ = value;
                SetProperty(ref KDPM_Logs_, KDPM_Logs);
            }
        }
        public List<Kd_SendLog> Kd_SendLogs_;
        public List<Kd_SendLog> Kd_SendLogs
        {
            get { return Kd_SendLogs_; }
            set
            {
                Kd_SendLogs_ = value;
                SetProperty(ref Kd_SendLogs_, Kd_SendLogs);
            }
        }
        private string Billid_;

        public string Billid
        {
            get { return Billid_; }
            set { Billid_ = value; }
        }

        #endregion
    }
}
